package com.quanyan.runningriding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.runningriding.entity.db.TbRunMusicCollection;
import com.quanyan.runningriding.entity.db.TbRunMusicType;
import com.quanyan.runningriding.entity.db.TbRunMusicTypeExample;
import com.quanyan.runningriding.entity.vo.run.MusicVo;
import com.quanyan.runningriding.entity.vo.run.req.RunMusicFMVo;
import com.quanyan.runningriding.entity.vo.run.resp.MusicFMVo;
import com.quanyan.runningriding.mapper.RunMusicMapper;
import com.quanyan.runningriding.mapper.TbRunMusicTypeMapper;
import com.quanyan.runningriding.service.RunMusicFMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liubin on 2016/11/23.
 */

@Service
public class RunMusicFMServiceImpl implements RunMusicFMService {
    @Autowired
    private RunMusicMapper runMusicMapper;

    @Autowired
    private TbRunMusicTypeMapper tbRunMusicTypeMapper;

    @Override
    public PageObj<List<TbRunMusicType>> musicFMList(Byte type, String typeName, String describeDetail,Integer pageNum,Integer pageSize) {
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("pageStart", (pageNum - 1) * pageSize);
        paramMap.put("pageSize", pageSize);
        paramMap.put("type",type);
        paramMap.put("typeName",typeName);
        paramMap.put("describeDetail",describeDetail);
        TbRunMusicTypeExample tbRunMusicTypeExample=new TbRunMusicTypeExample();

        TbRunMusicTypeExample.Criteria criteria = tbRunMusicTypeExample.createCriteria()
                .andTypeEqualTo(type);
        if (!StringUtil.isBlankOrNull(typeName)) {
            criteria.andTypeNameLike("%" + typeName + "%");
        }
        if (!StringUtil.isBlankOrNull(describeDetail)) {
            criteria.andDescribeDetailLike("%" + describeDetail + "%");
        }
        int count = tbRunMusicTypeMapper.countByExample(tbRunMusicTypeExample);
        Page page = new Page((pageNum - 1) * pageSize, pageSize);
        tbRunMusicTypeExample.setPage(page);
        List<TbRunMusicType> tbRunMusicTypes= tbRunMusicTypeMapper.selectByExample(tbRunMusicTypeExample);

        PageObj pageObj = PageObj.create(count, pageNum, pageSize, tbRunMusicTypes);
        return pageObj;
    }

    @Override
    public APIResponse<?> updateMusicFM(MusicFMVo musicFMVo) {
        TbRunMusicType tbRunMusicType = new TbRunMusicType();
        tbRunMusicType.setId(musicFMVo.getId());
        tbRunMusicType.setType(musicFMVo.getType());
        tbRunMusicType.setTypeName(musicFMVo.getTypeName());
        tbRunMusicType.setMinPace(musicFMVo.getMinPace());
        tbRunMusicType.setMaxPace(musicFMVo.getMaxPace());
        tbRunMusicType.setMinCadence(musicFMVo.getMinCadence());
        tbRunMusicType.setMaxCadence(musicFMVo.getMaxCadence());
        tbRunMusicType.setDescribeDetail(musicFMVo.getDescribeDetail());
        tbRunMusicType.setIsDelete(musicFMVo.getIsDelete());
        tbRunMusicType.setCreateTime(musicFMVo.getCreateTime());
        tbRunMusicType.setUpdateTime(musicFMVo.getUpdateTime());
        Integer i = tbRunMusicTypeMapper.updateByPrimaryKeySelective(tbRunMusicType);
        if (i > 0)
            return APIResponse.returnSuccess("跟新成功");
        return APIResponse.returnFail("跟新异常");
    }

    @Override
    public APIResponse<?> deleteMusicFM(Integer musicTypeId) {
        if (musicTypeId == 0 || musicTypeId == null) {
            APIResponse.returnFail("参数异常");
        }
        Integer i = tbRunMusicTypeMapper.deleteByPrimaryKey(musicTypeId);
        if (i > 0){
            return APIResponse.returnSuccess("删除成功");
        }
        return APIResponse.returnFail("删除异常");
    }

    @Override
    public APIResponse<?> insertMusicFM(RunMusicFMVo runMusicFMVo) {
        TbRunMusicType tbRunMusicType = new TbRunMusicType();
        tbRunMusicType.setId(runMusicFMVo.getId());
        tbRunMusicType.setType(runMusicFMVo.getType());
        tbRunMusicType.setTypeName(runMusicFMVo.getTypeName());
        int reuslt = tbRunMusicTypeMapper.insertSelective(tbRunMusicType);
        if (reuslt > 0){
            return APIResponse.returnSuccess("新增成功");
        }
        return APIResponse.returnFail("新增异常");
    }
}
